﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Globalization;

namespace SQLServerOptimizer.PL.WPF
{
    class ValidateTime : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {

            var str = value as string;
            if (String.IsNullOrEmpty(str))
            {
                return new ValidationResult(false,
                  "Please enter a start time");
            }

            var parts = str.Split(':');
            if (parts.Length != 2)
            {
                return new ValidationResult(false,
                  "use hh:mm format for the start time");
            }

            for (int i = 0; i < parts.Length; i++)
            {
                var p = parts[i];

               
                    int intPart;
                    if (!int.TryParse(p, NumberStyles.Integer,
                      cultureInfo.NumberFormat, out intPart))
                    {
                        return new ValidationResult(false,
                          "use numbers to define the time");
                    }

                    if ((intPart < 0 || intPart > 23) && i == 0)
                    {
                        return new ValidationResult(false,
                          "Hours should be between 00 and 23");
                    }
                    if ((intPart < 0 || intPart > 59) && i == 1)
                    {
                        return new ValidationResult(false,
                          "minutes should be between 00 and 59");
                    }


                
            }


            return new ValidationResult(true, null);
        }
    }
}

